package com.credithc.fastdfs.monitor.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.csource.common.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.credithc.fastdfs.api.entity.FdfsInfo;
import com.credithc.fastdfs.api.entity.Group;
import com.credithc.fastdfs.monitor.jsonbean.Columns;
import com.credithc.fastdfs.monitor.jsonbean.Line;
import com.credithc.fastdfs.monitor.service.FdfsInfoService;
import com.credithc.fastdfs.monitor.service.MonitorService;

/**
 * @ClassName: MonitorController
 * @Description: 监控模块
 * @author zhaoc
 * @date 2015年5月20日
 * @time 上午11:23:54
 */
@Controller
@RequestMapping(value = "/monitor")
public class MonitorController {
	private static final Logger log = LoggerFactory
			.getLogger(MonitorController.class);

	@Resource(name = "monitorServiceImpl")
	private MonitorService monitorServiceImpl;
	
	@Resource(name = "fdfsInfoServiceImpl")
	public FdfsInfoService fdfsInfoServiceImpl;

	/**
	 * @Title: performance
	 * @Description: 性能监控
	 * @param
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping(value = "/performance")
	public ModelAndView performance() throws IOException, MyException {
		ModelAndView mv = new ModelAndView("monitor/performance");
		List<Group> groupList = monitorServiceImpl.listGroupInfo();
		mv.addObject("groups", groupList);

		for (Group g : groupList) {
			log.info(g.getGroupID() + " | " + g.getGroupName());
		}
		return mv;
	}

	/**
	 * @Title: getPerformanceMemLine
	 * @Description: 内存折线图
	 * @param
	 * @return List<Line>
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getPerformanceMemLine")
	public List<Line> getPerformanceMemLine(
			@RequestParam("groupName") String groupName) throws IOException,
			MyException {
		return monitorServiceImpl.listStorageMemLines(groupName);
	}

	/**
	 * @Title: getPerformanceCupLine
	 * @Description: CPU折线图
	 * @param
	 * @return List<Line>
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getPerformanceCpuLine")
	public List<Line> getPerformanceCupLine(
			@RequestParam("groupName") String groupName) throws IOException,
			MyException {
		return monitorServiceImpl.listStorageCpuLines(groupName);
	}

	/**
	 *@date 2015年5月21日
	 *@author checkSun
	 *@description 流量监控
	 * @return
	 * @throws MyException 
	 * @throws IOException 
	 */
	@RequestMapping("/netTraffic")
	public ModelAndView netTraffic() throws IOException, MyException {
		ModelAndView mv = new ModelAndView("monitor/netTraffic");
		List<Group> groupList = monitorServiceImpl.listGroupInfo();
		mv.addObject("groups", groupList);
		return mv;
	}

	/**
	 *@date 2015年5月21日
	 *@author checkSun
	 *@description 流量监控数据
	 * @param ip
	 * @param start
	 * @param end
	 * @return
	 * @throws MyException 
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/getNetTrafficLine")
	public List<Line> getNetTrafficLine(@RequestParam("ip") String ip, @RequestParam("start")String start, @RequestParam("end") String end, @RequestParam("searchType") String searchType) throws IOException, MyException {
		return monitorServiceImpl.getNetTrafficLines(ip, start, end, searchType);
	}

	/**
	 * @Title: baseInfo
	 * @Description: 基础信息
	 * @param
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping(value = "/baseInfo")
	public ModelAndView baseInfo() throws IOException, MyException {
		ModelAndView mv = new ModelAndView("monitor/baseInfo");
		List<Group> groupList = monitorServiceImpl.listGroupInfo();
		mv.addObject("groups", groupList);
		return mv;
	}
	
	/** 
	 * @Title: capacity 
	 * @Description: 容量监控折线图
	 * @param 
	 * @return ModelAndView
	 * @throws 
	 */ 
	@RequestMapping(value = "/capacityLine")
	public ModelAndView capacityLine() throws IOException, MyException {
		ModelAndView mv = new ModelAndView("monitor/capacityLine");
		List<Group> groupList = monitorServiceImpl.listGroupInfo();
		mv.addObject("groups", groupList);
		return mv;
	}
	/** 
	 * @Title: getCapacityLine 
	 * @Description: 容量监控折线图数据
	 * @param 
	 * @return ModelAndView
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "/getCapacityLine")
	public List<Line> getCapacityLine(@RequestParam("ip") String ip, @RequestParam("start")String start, @RequestParam("end") String end, @RequestParam("searchType") String searchType) throws IOException, MyException {
		return monitorServiceImpl.getCapacityLines(ip, start, end,searchType);
	}

	/**
	 *@date 2015年5月25日
	 *@author checkSun
	 *@description 容量饼图信息
	 * @return
	 */
	@RequestMapping("/capacity")
	public ModelAndView capacity() {
		ModelAndView mv = new ModelAndView("monitor/capacity");
		try {
			mv.addObject("groupInfo", monitorServiceImpl.listGroupInfo());
		} catch (IOException e) {
			log.error(e.getMessage());
		} catch (MyException e) {
			log.error(e.getMessage());
		}

		return mv;
	}
	
	/** 
	 * @Title: fileCount 
	 * @Description: 上传下载文件数量统计
	 * @param 
	 * @return ModelAndView
	 * @throws 
	 */ 
	@RequestMapping("/filecount")
	public ModelAndView fileCount() throws IOException, MyException {
		ModelAndView mv = new ModelAndView("monitor/filecount");
		List<Group> groupList = monitorServiceImpl.listGroupInfo();
		mv.addObject("groups", groupList);
		return mv;
	}
	
	/** 
	 * @Title: getFileCountLine 
	 * @Description: 文件数量图
	 * @param 
	 * @return List<Line>
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping("/getFileCountLine")
	public List<Line> getFileCountLine(@RequestParam("ip") String ip, @RequestParam("start")String start, @RequestParam("end") String end, @RequestParam("searchType") String searchType) throws IOException, MyException {
		return monitorServiceImpl.getFileCountLines(ip, start, end, searchType);
	}
	
	/** 
	 * @Title: systemFilecount 
	 * @Description: 按系统柱状图统计文件数量
	 * @param 
	 * @return ModelAndView
	 * @throws 
	 */ 
	@RequestMapping("/systemFilecount")
	public ModelAndView systemFileCount() throws IOException, MyException {
		ModelAndView mv = new ModelAndView("monitor/systemfilecount");
		List<FdfsInfo> infos = fdfsInfoServiceImpl.getSystemFileCountType();
		mv.addObject("groups", infos);
		return mv;
	}
	
	/** 
	 * @Title: getSystemFileCountCol 
	 * @Description: 按系统柱状图统计文件数量
	 * @param 
	 * @return List<Line>
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping("/getSystemFileCountCol")
	public List<Columns> getSystemFileCountCol(@RequestParam("start")String start, @RequestParam("end") String end) throws IOException, MyException {
		List<Columns> colList = new ArrayList<Columns>();
		List<FdfsInfo> infos = fdfsInfoServiceImpl.getSystemFileCountCol(start, end);
		Columns upCol = new Columns("文件上传数量");
		Columns dwCol = new Columns("文件下载次数");
		List<Object> upsum = new ArrayList<Object>();
		List<Object> dwsum = new ArrayList<Object>();
		for(FdfsInfo info : infos) {
			upsum.add(info.getFileTotal());
			dwsum.add(info.getFileDownloadTotal());
		}
		upCol.setData(upsum);
		dwCol.setData(dwsum);
		colList.add(upCol);
		colList.add(dwCol);
		return colList;
	}
}
